﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
using Galaktika.BI.Extensibility.Dom.Dac;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess;
using Ranet.Olap.Core.Data;
using Ranet.Olap.Core;
using Ranet.Olap.Core.Providers.ClientServer;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands
{
    public class ExecuteQueryCommand : Galaktika.BI.Silverlight.Commands.ServerCommand
    {
        public ExecuteQueryCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.ExecuteQueryCommandId)
        {
            this.ContextId = invoker[KnownInvokeArgs.ContextId];
            this.DataSourceId = invoker[KnownInvokeArgs.DataSourceId];
            this.ConnectionId = invoker[KnownInvokeArgs.ConnectionId];
            this.Args = invoker[KnownInvokeArgs.ExecuteQueryCommand];
        }

        public string ContextId { get; private set; }
        public string ConnectionId { get; private set; }
        public string DataSourceId { get; private set; }
        public string Args { get; private set; }

        private IBindingContext Context
        {
            get
            {
                return DataBinder.Current.GetContext(this.ContextId);
            }
        }

        public override InvokeResultSchema Invoke()
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            res.ContentType = KnownContentTypes.MultidimData;

            MdxQueryArgs args = XmlUtility.XmlStr2Obj<MdxQueryArgs>(this.Args);
            if (args != null)
            {
                var exe = (IExecutionContext)this.Context.Services.GetService(typeof(IExecutionContext));
                if (exe != null)
                {
                    string query = exe.Parse(this.Context, args.Query);
                    string connStr = null;
                    BrickConnection conn = DataBinder.Current.GetNamedObject(this.ConnectionId) as BrickConnection;
                    if (conn != null)
                    {
                        connStr = exe.Parse(this.Context, conn.ConnectionString);
                        if (args.Type == QueryExecutingType.Query)
                        {
                            CellSetData csd = OlapQueryExecutor.ExecuteQuery(connStr, query, this.DataSourceId, this.Context).Data;
                            res.Content = CellSetData.Serialize(csd);
                            //res.Content = XmlUtility.Obj2XmlStr(csd, Common.Namespace);
                        }
                        if (args.Type == QueryExecutingType.NonQuery)
                        {
                            int rez = OlapQueryExecutor.ExecuteNonQuery(connStr, query, this.DataSourceId).Data;
                            res.Content = XmlUtility.Obj2XmlStr(rez, Common.Namespace);
                        }
                    }
                }
            }

            return res;
        }
    }
}
